package com.selfdriving.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.activity.FeatureDetailsActivity;
import com.selfdriving.activity.RegisterActivity;
import com.selfdriving.activity.TravelNotes;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.FindScenic;
import com.selfdriving.map.ScenicLocation;

public class SearchAdapter extends BaseAdapter
{
	private Context context;

	private List<Entity> features;

	private HolderView holderView;
	BaseApplication application;

	public SearchAdapter(BaseApplication application, Context context,
			List<Entity> features)
	{
		this.context = context;
		this.features = features;
		this.application = application;
	}

	@Override
	public int getCount()
	{
		return features.size();
	}

	@Override
	public Object getItem(int position)
	{
		return features.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int arg0, View view, ViewGroup arg2)
	{
		if (view == null)
		{
			holderView = new HolderView();
			view = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.feature_guide_item, null);
			view.setTag(holderView);
		} else
		{
			holderView = (HolderView) view.getTag();
		}
		FindScenic scenic = (FindScenic) features.get(arg0);
		holderView.guide = (Button) view.findViewById(R.id.btnGuide);
		holderView.guide.setOnClickListener(new OnMapClickListner(Double
				.parseDouble(scenic.getLat()), Double.parseDouble(scenic
				.getLng())));
		holderView.discuess = (Button) view.findViewById(R.id.btnDiscuss);
		holderView.strategy = (Button) view.findViewById(R.id.btnStrategy);
		holderView.icon = (ImageView) view.findViewById(R.id.ivBigImg);
		holderView.title = (TextView) view.findViewById(R.id.title);
		holderView.name = (TextView) view.findViewById(R.id.featrue_name);
		holderView.money = (TextView) view.findViewById(R.id.money);
		holderView.discount = (TextView) view.findViewById(R.id.discount);
		holderView.comment_button_text = (TextView) view
				.findViewById(R.id.comment_button_text);
		holderView.strategy
				.setOnClickListener(new OnFeatureDetailsClickListner(scenic
						.getName(), scenic.getId()));
		holderView.discuess.setOnClickListener(new OnTravelNotesClickListner(
				scenic.getName(), scenic.getId()));
		holderView.title.setText(scenic.getName());
		holderView.name.setText(scenic.getProvince());
		holderView.money.setText("￥" + scenic.getPrice() + "元");
		holderView.discount.setText(scenic.getPolicy());
		holderView.comment_button_text.setText(scenic.getClicks());
		application.imageLoader.displayImage(scenic.getImg0(), holderView.icon,
				application.options);
		return view;
	}

	private class HolderView
	{
		private ImageView icon;
		private TextView title;
		private TextView name;
		private TextView money;
		private TextView discount;
		private TextView comment_button_text;
		private Button guide;
		private Button discuess;
		private Button strategy;
	}

	public class OnMapClickListner implements OnClickListener
	{
		double lat = 0;
		double lng = 0;

		public OnMapClickListner(double lat, double lng)
		{
			this.lat = lat;
			this.lng = lng;
		}

		@Override
		public void onClick(View v)
		{
			if (checkIsLoading())
			{
				application.geoPoint = new GeoPoint((int) (lng * 1e6),
						(int) (lat * 1e6));
				Intent intent = new Intent(context, ScenicLocation.class);
				context.startActivity(intent);
			}
		}
	}

	public class OnTravelNotesClickListner implements OnClickListener
	{
		String name;
		String sid;

		public OnTravelNotesClickListner(String name, String sid)
		{
			this.name = name;
			this.sid = sid;
		}

		@Override
		public void onClick(View v)
		{
			if (checkIsLoading())
			{
				Intent intent = new Intent(context, TravelNotes.class);
				intent.putExtra("title", name);
				intent.putExtra("sid", sid);
				context.startActivity(intent);
			}
		}
	}

	public class OnFeatureDetailsClickListner implements OnClickListener
	{
		String sid;
		String name;

		public OnFeatureDetailsClickListner(String name, String sid)
		{
			this.sid = sid;
			this.name = name;
		}

		@Override
		public void onClick(View v)
		{
			if (checkIsLoading())
			{
				Intent intent = new Intent(context,
						FeatureDetailsActivity.class);
				intent.putExtra("sid", sid);
				intent.putExtra("name", name);
				context.startActivity(intent);
			}
		}
	}

	public boolean checkIsLoading()
	{
		if (application.user == null)
		{
			Intent intent = new Intent(context, RegisterActivity.class);
			context.startActivity(intent);
			return false;
		} else
		{
			return true;
		}
	}
}
